<!DOCTYPE HTML>
<html>
<HEAD>
<meta charset="utf-8">
<TITLE>员工添加/修改</TITLE>
</HEAD>
<body>
    
<?php
include_once("base-class.php");
include_once('DB.php');

$DB = new DB();

//获取需要修改的员工ID号传入
$roster_id=intval($_GET["roster_id"]);

//获取操作标识传入
$action=$_POST["action"];
$action= string::un_script_code($action);
$action= string::un_html($action);

//判断是否修改，如果传入了员工ID，进行数据库查询获取全部内容
if($roster_id)
{
        $roster_value=$DB->get_one("select * from roster where roster_id={$roster_id}");
        
        if(!$roster_value)
	{
		echo "<script>alert('无此员工');history.back();</Script>";
		exit;
	}
}

//如果获取到操作标识，进行录入或者修改操作
if($action=="update")
{
    //获取表单传入数据
	$old_roster_id=$_POST["roster_id"];
        $old_roster_pic=$_POST["old_roster_pic"];
	$roster_name=$_POST["roster_name"];
        $roster_rname = $_POST["roster_rname"];
	$roster_number=$_POST["roster_number"];
	$roster_sex=$_POST["roster_sex"];
        $roster_title=$_POST["roster_title"];
       
	$roster_birthday=$_POST["roster_birthday"];
	$roster_mp=$_POST["roster_mp"];
	$roster_phone=$_POST["roster_phone"];
	$roster_class=$_POST["roster_class"];
	$roster_mail=$_POST["roster_mail"];
	$roster_wechat=$_POST["roster_wechat"];
        $roster_status=$_POST["roster_status"];
	$roster_in=$_POST["roster_in"];

    //传入数据过滤
    $old_roster_id=intval($old_roster_id);
    $old_roster_pic= string::un_script_code($old_roster_pic);
    $roster_name= string::un_script_code($roster_name);
    $roster_rname= string::un_script_code($roster_rname);
    
    $roster_number= string::un_script_code($roster_number);
    $roster_sex= intval($roster_sex);
    $roster_title=intval($roster_title);
     switch($roster_title){
            case 1:
                $roster_title = "实习生";
               break;
            case 2:
                  $roster_title = "员工";
                break;
            case 3:
                  $roster_title = "主管";
                break;
            case 4:
                  $roster_title = "总监";
                break;
            case 5:
                  $roster_title = "经理";
                break;
            default:
                 $roster_title = "员工";
                break;
        }
    
    $roster_birthday= string::un_script_code($roster_birthday);
    $roster_mp= string::un_script_code($roster_mp);
    $roster_phone= string::un_script_code($roster_phone);
    $roster_class= intval($roster_class);
    $roster_mail= string::un_script_code($roster_mail);
    $roster_wechat= string::un_script_code($roster_wechat);
    $roster_status= intval($roster_status);
    $roster_in= string::un_script_code($roster_in);
    
    //检测必填项目
    //if(!$roster_name || !$roster_number || !$roster_sex || !$roster_birthday || !$roster_mp 
    //   || !$roster_class || !$roster_mail || !$roster_status)
    //{
	//	echo "<script>alert('表单未填完整，带星号为必填项！');history.back();</Script>";
	//	exit;    
    //}
    
    //监测手机号
    
    if(!string::is_mobile($roster_mp))
    {
		echo "<script>alert('手机号码不正确，请重新输入！');history.back();</Script>";
		exit;    
    }
    //监测邮箱
    
    if(!string::is_mail($roster_mail))
    {
		echo "<script>alert('邮箱格式不正确，请重新输入！');history.back();</Script>";
		exit;    
    }
    
    //检测照片上传，如果没有上传照片并且不是修改状态即无旧照片数据时则提示
	if($_FILES['roster_pic']['name']=='' && $old_roster_pic=='')
	{
            //如果没有上传头像，则使用默认的头像
          $old_roster_pic = "http://wechat.zhijia365.com/upload/roster/touxiang.jpg";
		//echo "<script>alert('请上传图片!');history.back();</Script>";
		//exit;
	}
                //照片上传
        if($_FILES['roster_pic']['name']!='')
          {
            
                 //上传文件类型列表  
                $uptypes=array(  
                 'image/jpg',  
                 'image/jpeg',  
                 'image/png',  
                 'image/pjpeg',  
                 'image/gif',  
                 'image/bmp',  
                 'image/x-png'  
                );  
  
                $max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
                $destination_folder="/data/wechat/public_html/upload/img/"; //上传文件路径  
                
                $file = $_FILES['roster_pic'];  
                //图片文件链接
                $picFileName = "";
                if (!is_uploaded_file($_FILES["roster_pic"]["tmp_name"]))  
                //是否存在文件  
                {  
                     echo "图片不存在，文件上传失败!";  
                    // exit;  
                }     //检查文件大小  
               if($max_file_size < $file["size"]) 
                {  
                    echo "<script>alert('文件太大!');</script>";  
                    //exit;  
                 } 
               
                 if(!in_array($file["type"], $uptypes))  
                //检查文件类型  
                {  
                     echo "aaaaaaa".$file["type"];
                     echo "<script>alert('文件类型不符!');</script>".$file["type"];  
                    //exit;  
                }  
                if(!file_exists($destination_folder))  
                {  
                    if(mkdir($destination_folder)){
                        echo "<script>alert('成功创建文件夹');</script>";  
                    }else{
                        echo "<script>alert('创建文件夹失败');</script>";  
                        //exit;  
                    }
                }  

                $filename=$file["tmp_name"];  
                $image_size = getimagesize($filename);  
                $pinfo=pathinfo($file["name"]);  
                $ftype=$pinfo['extension'];  
                $destination = $destination_folder.time().".".$ftype;  

                if (file_exists($destination) && $overwrite != true)  
                {  
                    echo "<script>alert('同名文件已经存在了');</script>";  
                  //  exit;  
                }  

                if(!move_uploaded_file ($filename, $destination))  
                {  
                    echo "<script>alert('移动文件出错');</script>";  
                    //exit;  
                }  

                
                if($ftype!=null){
                //保存图片文件的链接地址
                 $old_roster_pic = "http://wechat.zhijia365.com/upload/img/".time().".".$ftype; 
                }else {
                  $old_roster_pic = "http://wechat.zhijia365.com/upload/roster/touxiang.jpg";    
                }
        
        
        
//        //允许上传文件类型
//        $file_ext_arr=array("jpg","png");
//        
//        //检测上传文件后缀    
//        $get_exts = explode('.',$_FILES['roster_pic']['name']);
//        $exts_len=count($get_exts);
//        $file_ext=strtolower($get_exts[$exts_len-1]);
//        
//        if(!in_array($file_ext,$file_ext_arr))
//        {
//            echo "<script>alert('只允许上传后缀为jpg或者png的图片文件!');history.back();</Script>";
//            //exit;
//        }
//        //设定新文件名称
//        $new_filename=substr(md5($roster_number),0,6).date("YmdHis").".".$file_ext;
//        
//            //保存图片文件的链接地址
//        $picFileName = "http://wechat.zhijia365.com/upload/img/".$new_filename;
//        
//      
//        //上传到SAE的Storage里，注意修改存储空间的名字“weixincourse”，并把返回的文件名赋值给旧文件变量old_roster_pic
//        $old_roster_pic=$s->upload( 'weixincourse' , $new_filename , $_FILES['roster_pic']['tmp_name'] );   
    }  
    
    //默认参数
    $nowtime=date("Y/m/d H:i:s",time());
    //如果是修改
    if($old_roster_id)
    {
 
        $sql = "update roster set 
        roster_name='$roster_name',
        roster_rname ='$roster_rname',
        roster_number='$roster_number',
        roster_pic='$old_roster_pic',
        roster_sex='$roster_sex',
        roster_title = '$roster_title',
        roster_birthday='$roster_birthday',
        roster_mp='$roster_mp',
        roster_phone='$roster_phone',
        roster_class='$roster_class',
        roster_mail='$roster_mail',
        roster_wechat='$roster_wechat',
        roster_status='$roster_status',
        roster_in='$roster_in',
        edittime='$nowtime'
        where roster_id=$old_roster_id";
 		$bool = $DB->query( $sql );
                
                   if($bool){
                    echo "<script>alert('员工修改成功');</script>";
              
                }else {
                     echo "<script>alert('员工修改失败');</script>";
                }
    }
    else
    {
        //新增
   		$sql = "insert into roster 
        (roster_name,roster_rname,roster_number,roster_pic,roster_sex,roster_title,
        roster_birthday,roster_mp,roster_phone,roster_class,
        roster_mail,roster_wechat,roster_status,roster_in,addtime,edittime,status) 
        values 
        ('$roster_name','$roster_rname','$roster_number','$old_roster_pic','$roster_sex','$roster_title',
        '$roster_birthday','$roster_mp','$roster_phone','$roster_class',
        '$roster_mail','$roster_wechat','$roster_status','$roster_in','$nowtime','$nowtime',1)";
 		$bool = $DB->query( $sql );
                
                if($bool){
                    echo "<script>alert('员工添加成功');</script>";
              
                }else {
                     echo "<script>alert('员工添加失败');</script>";
                }
   	
    }

    
}    

//获取所有部门
//$class_list=$mysql->getData("select class_name,class_id from class where status=1 order by class_fid asc");
$class_list = $DB->get_all("select class_name,class_id from class");

?>
     <h1>华视文化公司通讯录管理</h1>
    <!--页面名称-->
	<h3>员工添加/修改<a href="roster_manager.php">返回>></a></h3>
    <p style="color:red;font-weight:bold;">请在电脑上使用该后台，否则无法上传照片……</p>
    <p>带星号为必填项</p>
    <!--表单开始-->
    <form action="" method="post" name="roster_add" id="roster_add" enctype="multipart/form-data">
        <p>
            <b>*</b>姓名：<input type="text" value="<?php echo $roster_value["roster_name"];?>" name="roster_name">如：李桦
        </p>
           <p>
            <b>*</b>别名：<input type="text" value="<?php echo $roster_value["roster_rname"];?>" name="roster_rname">姓名的第一个字母，如：lh
        </p>
        <p>
            <b>*</b>工号：<input type="text" value="<?php echo $roster_value["roster_number"];?>" name="roster_number">
        </p>
        
        <p>
            <b>*</b>职位： 
            <select name="roster_title">
                <option value="0">请选择职位</option>
                <option <?php if($roster_value["roster_sex"]==1) echo "selected"?> value="1">实习生</option>
                <option <?php if($roster_value["roster_sex"]==2) echo "selected"?>  value="2">员工</option>
                <option <?php if($roster_value["roster_sex"]==3) echo "selected"?>  value="3">主管</option>
                <option <?php if($roster_value["roster_sex"]==4) echo "selected"?>  value="4">总监</option>
                <option <?php if($roster_value["roster_sex"]==5) echo "selected"?>  value="5">经理</option>
            </select>
        </p>
        
        
        <p>
            <b>*</b>照片：<input type="file"  name="roster_pic">可上传jpg或者png图片
            <?php
				//如果是修改有照片则显示照片，并把旧照片名称存放到一个隐藏输入框里
            	if($roster_value["roster_pic"])
                {
                    echo "<br><img src=\"{$roster_value[roster_pic]}\" height=150>";
                    echo "<input type=\"hidden\" name=\"old_roster_pic\" value=\"{$roster_value[roster_pic]}\">";
                }
            ?>
        </p>
        <p>
            <b>*</b>性别：
            <select name="roster_sex">
                <option value="0">请选择性别</option>
                <option <?php if($roster_value["roster_sex"]==1) echo "selected"?> value="1">男</option>
                <option <?php if($roster_value["roster_sex"]==2) echo "selected"?>  value="2">女</option>
            </select>
        </p>
        <p>
            <b>*</b>生日：<input type="date" value="<?php echo $roster_value["roster_birthday"];?>" name="roster_birthday">
        </p>
        <p>
            <b>*</b>手机：<input type="text" value="<?php echo $roster_value["roster_mp"];?>" name="roster_mp">
        </p>
        <p>
            电话：<input type="text" value="<?php echo $roster_value["roster_phone"];?>" name="roster_phone">
        </p>
        <p>
            <b>*</b>部门：
            <select name="roster_class">
                <option value="0">请选择部门</option>
                <?php
    				//把所有部门列表出来
                    foreach($class_list as $value)
                    {
                        $class_select=($roster_value["roster_class"]==$value["class_id"])?" selected":"";
                        echo "<option value=\"$value[class_id]\" $class_select>$value[class_name]</option>";
                    }
                ?>
            </select>
        </p>
        <p>
            <b>*</b>邮箱：<input type="text" value="<?php echo $roster_value["roster_mail"];?>" name="roster_mail">
        </p>
        <p>
            微信：<input type="text" value="<?php echo $roster_value["roster_wechat"];?>" name="roster_wechat">
        </p>
        <p>
            <b>*</b>状态：
            <select name="roster_status">
                <option value="0">请选择员工状态</option>
                <option <?php if($roster_value["roster_status"]==1) echo "selected"?> value="1">在职</option>
                <option <?php if($roster_value["roster_status"]==2) echo "selected"?>  value="2">休假</option>
                <option <?php if($roster_value["roster_status"]==3) echo "selected"?>  value="3">试用期</option>
                <option <?php if($roster_value["roster_status"]==4) echo "selected"?>  value="4">离职</option>
            </select>
        </p>
        <p>
            入职：<input type="date" value="<?php echo $roster_value["roster_in"];?>" name="roster_in">
        </p>
         <p>
             <!--隐藏参数，用来放置操作标示和修改的ID-->
            <input type="hidden" name="action"  value="update">
            <input type="hidden" name="roster_id" value="<?=$roster_value["roster_id"]?>">
             <!--表单提交-->
            <input type="submit" value="提交" />
        </p>
    </form>
</body>
</html>
